﻿//
// Settings.cs: Abstract definition of a settings class.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Reflection;

namespace S9PalCommon
{
    /// <summary>
    /// Abstract definition of a settings class.
    /// </summary>
    [Serializable]
    public abstract class Settings
    {
        #region Fields

        private String fileName;

        #endregion

        #region Constructors

        public Settings(String fileName)
        {
            this.fileName = fileName;

            // Check if the settings file exists. If not, apply the default settings and save them to the file.
            if (!File.Exists(S9PalCommon.Global.SettingsDirectoryPath + fileName))
            {
                ApplyDefaultSettings();
                SaveSettings();
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// File name that the settings are saved to.
        /// </summary>
        public String FileName
        {
            get
            {
                return fileName;
            }
            protected set
            {
                fileName = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Apply the default settings for the syncer.
        /// </summary>
        public void ApplyDefaultSettings()
        {
            OnDefault();
        }

        /// <summary>
        /// Serialize the current object.
        /// </summary>
        public void SaveSettings()
        {
            // Create a Type object representing class Helper and get a MethodInfo representing the generic method.
            Type helper = typeof(Helper);
            MethodInfo mi = helper.GetMethod("SerializeObject").MakeGenericMethod(this.GetType());

            // Invoke the method.
            object[] args = {S9PalCommon.Global.SettingsDirectoryPath + fileName, this};
            mi.Invoke(null, args);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Implements the default settings for the syncer.
        /// </summary>
        protected abstract void OnDefault();

        #endregion
    }
}